package com.pengtu.web.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.jbpm.api.history.HistoryTask;
import org.jbpm.pvm.internal.task.TaskImpl;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.Constants;
import com.pengtu.entity.app.User;
import com.pengtu.model.PersonalModel;
import com.pengtu.security.SpringSecurityUtils;
import com.pengtu.service.business.HomeManager;
import com.pengtu.utils.DateUtils;
import com.pengtu.utils.SortList;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Results({
		@Result(name = Constants.LABS, location = "${ctx}/business/labs!taketask.action?id=${key}&taskId=${taskId}", type = "redirect"),
		@Result(name = Constants.DETECTION, location = "${ctx}/business/detection!taketask.action?id=${key}&taskId=${taskId}", type = "redirect"),
		@Result(name = Constants.APPLY, location = "${ctx}/business/application!taketask.action?id=${key}&taskId=${taskId}", type = "redirect"),
		@Result(name = "convey", location = "${ctx}/admin/convey!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "received", location = "${ctx}/admin/received!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "apply", location = "${ctx}/business/application!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "detections", location = "${ctx}/business/detection!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "accept", location = "${ctx}/business/acceptance!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "comple", location = "${ctx}/business/completion!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "labs", location = "${ctx}/business/labs!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "plan", location = "${ctx}/quality/plan!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "check", location = "${ctx}/quality/check!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "mdetect", location = "${ctx}/business/middledetect!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "dmarine", location = "${ctx}/business/marine/detection/marine!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "cmarine", location = "${ctx}/business/marine/check/cmarine!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "recheck", location = "${ctx}/business/recheck!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "estimate", location = "${ctx}/cost/estimate/estimate!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "budget", location = "${ctx}/cost/budget/budget!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "limited", location = "${ctx}/cost/limited/limited!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "completed", location = "${ctx}/cost/completed/completed!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "modify", location = "${ctx}/cost/modify/modify!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "drawing", location = "${ctx}/cost/drawing/drawing!judge.action?id=${fid}", type = "redirect"),
		@Result(name = "leave", location = "${ctx}/human/leave!judge.action?id=${fid}", type = "redirect")})
public class HomeAction extends BaseActionSupport<PersonalModel> {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private HomeManager homeManager;

	private PersonalModel entity;
	private String taskId;
	private String key;
	private List<PersonalModel> homeModels;
	private String status;
	private String fid;

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getFid() {
		return fid;
	}

	public void setFid(String fid) {
		this.fid = fid;
	}

	public List<PersonalModel> getHomeModels() {
		return homeModels;
	}

	public void setHomeModels(List<PersonalModel> homeModels) {
		this.homeModels = homeModels;
	}

	@Autowired
	public void setHomeManager(HomeManager homeManager) {
		this.homeManager = homeManager;
	}

	public PersonalModel getModel() {
		return entity;
	}

	public String list() throws Exception {

		return null;
	}

	public String input() throws Exception {
		return INPUT;
	}

	public void prepareInput() throws Exception {
		User user = SpringSecurityUtils.getCurrentUser();
		List<TaskImpl> taskImpls = homeManager.findTask(user);
		SortList<TaskImpl> sortList = new SortList<TaskImpl>();
		sortList.Sort(taskImpls, "getCreateTime", "desc");
		List<PersonalModel> personalModels = new ArrayList<PersonalModel>();
		for (TaskImpl taskImpl : taskImpls) {
			String id = taskImpl.getExecutionId().split("\\.")[1];
			String key = (String) taskImpl.getVariable(Constants.START_TYPE);
			PersonalModel personalModel = homeManager.getObject(id, key);
			personalModel.setTaskImpl(taskImpl);
			personalModel.setKeyword(key);
			personalModel.setFid(id);
			personalModels.add(personalModel);
		}
		Struts2Utils.getRequest()
				.setAttribute("personalModels", personalModels);
	}

	/**
	 * 待办转发 view: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String view() throws Exception {
		String key = Struts2Utils.getParameter("key");
		if (StringUtils.equals("convey", key)) {
			return "convey";
		}
		if (StringUtils.equals("received", key)) {
			return "received";
		}
		if (StringUtils.equals("apply", key)) {
			return "apply";
		}
		if (StringUtils.equals("detection", key)) {
			return "detections";
		}
		if (StringUtils.equals("accept", key)) {
			return "accept";
		}
		if (StringUtils.equals("comple", key)) {
			return "comple";
		}
		if (StringUtils.equals("labs", key)) {
			return "labs";
		}
		if (StringUtils.equals("plan", key)) {
			return "plan";
		}
		if (StringUtils.equals("check", key)) {
			return "check";
		}
		if (StringUtils.equals("mdetect", key)) {
			return "mdetect";
		}
		if (StringUtils.equals("dmarine", key)) {
			return "dmarine";
		}
		if (StringUtils.equals("cmarine", key)) {
			return "cmarine";
		}
		if (StringUtils.equals("recheck", key)) {
			return "recheck";
		}
		if (StringUtils.equals("estimate", key)) {
			return "estimate";
		}
		if (StringUtils.equals("budget", key)) {
			return "budget";
		}
		if (StringUtils.equals("limited", key)) {
			return "limited";
		}
		if (StringUtils.equals("drawing", key)) {
			return "drawing";
		}
		if (StringUtils.equals("completed", key)) {
			return "completed";
		}
		if (StringUtils.equals("modify", key)) {
			return "modify";
		}
		if (StringUtils.equals("oconvey", key)) {
			return "convey";
		}
		if (StringUtils.equals("leave", key)) {
			return "leave";
		}
		return null;
	}

	/**
	 * 参与的流程 look: 适用:
	 * 
	 * @return
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public String look() throws Exception {
		User user = SpringSecurityUtils.getCurrentUser();
		List<HistoryTask> historyTasks = homeManager.getHistoryTasks(user);
		Struts2Utils.getRequest().setAttribute("historyTasks", historyTasks);
		return Constants.LOOK;
	}

	public String save() throws Exception {
		return null;
	}

	public String delete() throws Exception {
		return null;
	}

	/**
	 * 待办事宜 homeFront: 适用:首页待办事宜
	 * 
	 * @throws Exception
	 * @exception
	 * @since 1.0.0
	 */
	public void homeFront() throws Exception {
		List<TaskImpl> homeList = new ArrayList<TaskImpl>();
		User user = SpringSecurityUtils.getCurrentUser();
		homeList = homeManager.findTask(user);
		SortList<TaskImpl> sortList = new SortList<TaskImpl>();
		sortList.Sort(homeList, "getCreateTime", "desc");
		String root = Struts2Utils.getRootPath();
		String content = "<table class='frontContent'>";
		int index = 0;
		int a = homeList.size();
		if (a >= 7) {
			index = 7;
		} else {
			index = homeList.size();
		}
		for (int i = 0; i < index; i++) {
			TaskImpl homeModel = new TaskImpl();
			homeModel = homeList.get(i);
			String id = homeModel.getExecutionId().split("\\.")[1];
			String key = (String) homeModel.getVariable(Constants.START_TYPE);
			PersonalModel personalModel = homeManager.getObject(id, key);
			if(personalModel == null){
				continue;
			}
			String url = "";
			String script1 = "javascript:openAlone(\"";
			String script2 = "";
			if (StringUtils.equals("convey", key)) {
				url = root + "/admin/convey!judge.action?";
				script2 = "'>";
			}
			if (StringUtils.equals("leave", key)) {
				url = root + "/human/leave!judge.action?";
				script2 = "'>";
			}
			if (StringUtils.equals("oconvey", key)) {
				url = root + "/admin/convey!judge.action?";
				script2 = "'>";
			}
			if (StringUtils.equals("received", key)) {
				url = root + "/admin/received!judge.action?";
				script2 = "'>";
			}
			if (StringUtils.equals("apply", key)) {
				url = script1 + root + "/business/application!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("mdetect", key)) {
				url = script1 + root + "/business/middledetect!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("dmarine", key)) {
				url = script1 + root
						+ "/business/marine/detection/marine!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("cmarine", key)) {
				url = script1 + root
						+ "/business/marine/check/cmarine!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("detection", key)) {
				url = script1 + root + "/business/detection!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("accept", key)) {
				url = script1 + root + "/business/acceptance!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("comple", key)) {
				url = script1 + root + "/business/completion!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("recheck", key)) {
				url = script1 + root + "/business/recheck!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("labs", key)) {
				url = script1 + root + "/business/labs!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("plan", key)) {
				url = root + "/quality/plan!judge.action?";
				script2 = "'>";
			}

			if (StringUtils.equals("estimate", key)) {
				url = script1 + root + "/cost/estimate/estimate!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("budget", key)) {
				url = script1 + root + "/cost/budget/budget!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("limited", key)) {
				url = script1 + root + "/cost/limited/limited!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("drawing", key)) {
				url = script1 + root + "/cost/drawing/drawing!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("completed", key)) {
				url = script1 + root
						+ "/cost/completed/completed!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("modify", key)) {
				url = script1 + root + "/cost/modify/modify!judge.action?";
				script2 = "\")'>";
			}
			if (StringUtils.equals("check", key)) {
				url = root + "/quality/check!judge.action?projectId="
						+ personalModel.getProjectId() + "&";
				script2 = "'>";
			}
			if (StringUtils.equals("entrust", key)) {
				url = root + "/receive/check!judge.action?projectId="
						+ personalModel.getProjectId() + "&";
				script2 = "'>";
			}
			if (StringUtils.equals("order", key)) {
				url = root + "/measure/check!judge.action?projectId="
						+ personalModel.getProjectId() + "&";
				script2 = "'>";
			}
			content += "<tr><td class='front_news' ><a href='" + url + "id="
					+ id + script2
					+ (i + 1) + ".&nbsp;"
					+ DateUtils.convertDateToString(homeModel.getCreateTime())
					+ "&nbsp;" + personalModel.getTitle() + "<"
					+ homeModel.getActivityName() + ">" + " </a></td></tr>";
		}
		content += "</table>";
		if (a > 7) {
			content += "<div align='right'><a href='" + root
					+ "/business/home!input.action" + "'>更多</a></div>";
		}
		Map<String, Object> json = new HashMap<String, Object>();
		json.put("count", a);
		json.put("content", content);
		Struts2Utils.renderJson(json);
	}

	protected void prepareModel() throws Exception {

	}

}
